home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / WizardController.java < prev    next >
Text File  |  1998-10-08  |  14KB  |  757 lines

  1.  
  2. package com.symantec.itools.frameworks.wizard;
  3.  
  4.  
  5. import com.sun.java.swing.JPanel;
  6. import java.awt.Image;
  7. import java.io.InputStream;
  8. import java.io.IOException;
  9. import java.net.URL;
  10. import java.util.Enumeration;
  11. import java.util.Vector;
  12. import com.symantec.itools.swing.icons.ImageIcon;
  13.  
  14.  
  15. /**
  16.  * @author Symantec Internet Tools Division
  17.  * @version 1.0
  18.  * @since VCafe 3.0
  19.  */
  20.  
  21.  
  22. public class WizardController
  23. {
  24.     protected static final int NULL_PANEL = -2;
  25.  
  26.     /**
  27.      * @since VCafe 3.0
  28.      */
  29.     protected Wizard     wizard;
  30.  
  31.     /**
  32.      * @since VCafe 3.0
  33.      */
  34.     protected Vector     panels;
  35.  
  36.     /**
  37.      * @since VCafe 3.0
  38.      */
  39.     protected int        currentPanelIndex;
  40.  
  41.     /**
  42.      * @since VCafe 3.0
  43.      */
  44.     protected JPanel currentPanel;
  45.  
  46.     /**
  47.      * @since VCafe 3.0
  48.      */
  49.     protected String     title;
  50.  
  51.     {
  52.         currentPanelIndex = -1;
  53.         panels            = new Vector();
  54.     }
  55.  
  56.     public WizardController()
  57.     {
  58.     }
  59.  
  60.     public WizardController(Wizard w)
  61.     {
  62.         wizard = w;
  63.         wizard.setWizardController(this);
  64.     }
  65.  
  66.     public void setWizard(Wizard w)
  67.     {
  68.         wizard = w;
  69.         wizard.setWizardController(this);
  70.     }
  71.  
  72.     /**
  73.      * @since VCafe 3.0
  74.      */
  75.  
  76.     public void init()
  77.     {
  78.         if(panels.size() > 0)
  79.         {
  80.             currentPanelIndex = firstPanelIndex();
  81.             currentPanel      = (JPanel)panels.elementAt(currentPanelIndex);
  82.  
  83.             if(wizard != null)
  84.             {
  85.                 if(currentPanelIndex > 0)
  86.                 {
  87.                     wizard.setBackEnabled(true);
  88.                 }
  89.                 else
  90.                 {
  91.                     wizard.setBackEnabled(false);
  92.                 }
  93.  
  94.                 wizard.setCancelEnabled(true);
  95.                 wizard.setHelpEnabled(true);
  96.  
  97.                 if(panels.size() > 1 + currentPanelIndex)
  98.                 {
  99.                     wizard.setNextEnabled(true);
  100.                     wizard.setFinishEnabled(false);
  101.                 }
  102.                 else
  103.                 {
  104.                     wizard.setNextEnabled(false);
  105.                     wizard.setFinishEnabled(true);
  106.                 }
  107.  
  108.                 wizard.showPanel(currentPanel);
  109.             }
  110.  
  111.             if(currentPanel instanceof WizardPage)
  112.             {
  113.                 ((WizardPage)currentPanel).entering();
  114.             }
  115.         }
  116.         else
  117.         {
  118.             if(wizard != null)
  119.             {
  120.                 wizard.setBackEnabled(false);
  121.                 wizard.setNextEnabled(false);
  122.                 wizard.setCancelEnabled(true);
  123.                 wizard.setHelpEnabled(true);
  124.                 wizard.setFinishEnabled(true);
  125.             }
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * @param panel TODO
  131.      * @since VCafe 3.0
  132.      */
  133.  
  134.     public void addPanel(JPanel panel)
  135.     {
  136.         if(panel instanceof WizardPage)
  137.         {
  138.             ((WizardPage)panel).setWizardController(this);
  139.         }
  140.  
  141.         panels.addElement(panel);
  142.     }
  143.  
  144.     /**
  145.      * @param panel TODO
  146.      * @param index TODO
  147.      * @since VCafe 3.0
  148.      */
  149.  
  150.     public boolean addPanel(JPanel panel, int index)
  151.     {
  152.         if(panel instanceof WizardPage)
  153.         {
  154.             ((WizardPage)panel).setWizardController(this);
  155.         }
  156.  
  157.         if(index < 0)
  158.         {
  159.             panels.addElement(panel);
  160.         }
  161.         else
  162.         {
  163.             panels.insertElementAt(panel, index);
  164.         }
  165.  
  166.         return true;
  167.     }
  168.  
  169.     /**
  170.      * @param panel TODO
  171.      * @since VCafe 3.0
  172.      */
  173.  
  174.     public boolean removePanel(JPanel panel)
  175.     {
  176.         return panels.removeElement(panel);
  177.     }
  178.  
  179.     /**
  180.      * @param index TODO
  181.      * @since VCafe 3.0
  182.      */
  183.  
  184.     public boolean removePanel(int index)
  185.     {
  186.         if (index >= 0 && index < panels.size())
  187.         {
  188.             panels.removeElementAt(index);
  189.             return true;
  190.         }
  191.         else
  192.             return false;
  193.     }
  194.  
  195.     /**
  196.      * @since VCafe 3.0
  197.      */
  198.  
  199.     public Wizard getWizard()
  200.     {
  201.         return wizard;
  202.     }
  203.  
  204.     /**
  205.      * @since VCafe 3.0
  206.      */
  207.  
  208.     public void backPanel()
  209.     {
  210.         if(currentPanelIndex > 0)
  211.         {
  212.             currentPanelIndex = computePreviousPanelIndex(currentPanelIndex);
  213.             currentPanel = (JPanel)panels.elementAt(currentPanelIndex);
  214.  
  215.             if(currentPanelIndex > 0 && wizard != null)
  216.             {
  217.                 wizard.setBackEnabled(true);
  218.             }
  219.         }
  220.  
  221.         if(wizard != null)
  222.         {
  223.             if(currentPanelIndex == 0)
  224.             {
  225.                 wizard.setBackEnabled(false);
  226.             }
  227.  
  228.             if(panels.size() > currentPanelIndex+1)
  229.             {
  230.                 wizard.setNextEnabled(true);
  231.             }
  232.  
  233.             wizard.setFinishEnabled(false);
  234.             wizard.showPanel(currentPanel);
  235.         }
  236.  
  237.         if(currentPanel instanceof WizardPage)
  238.         {
  239.             ((WizardPage)currentPanel).entering();
  240.         }
  241.     }
  242.  
  243.     /**
  244.      * @since VCafe 3.0
  245.      */
  246.     public void nextPanel()
  247.     {
  248.         if(!(currentPanel instanceof WizardPage) || (((WizardPage)currentPanel).exiting()))
  249.         {
  250.             currentPanelIndex = computeNextPanelIndex(currentPanelIndex);
  251.  
  252.             if(wizard != null)
  253.             {
  254.                 wizard.setBackEnabled(true);
  255.             }
  256.  
  257.             if(currentPanelIndex < panels.size())
  258.             {
  259.                 currentPanel = (JPanel)panels.elementAt(currentPanelIndex);
  260.             }
  261.         }
  262.  
  263.         if(currentPanelIndex == panels.size() - 1)
  264.         {
  265.             if(wizard != null)
  266.             {
  267.                 wizard.setBackEnabled(true);
  268.                 wizard.setNextEnabled(false);
  269.                 wizard.setFinishEnabled(true);
  270.             }
  271.         }
  272.         
  273.         if(wizard != null)
  274.         {
  275.             wizard.showPanel(currentPanel);
  276.         }
  277.  
  278.         if(currentPanel instanceof WizardPage)
  279.         {
  280.             ((WizardPage)currentPanel).entering();
  281.         }
  282.     }
  283.  
  284.     /**
  285.      * @since VCafe 3.0
  286.      */
  287.  
  288.     protected int firstPanelIndex()
  289.     {
  290.         return 0;
  291.     }
  292.  
  293.     /**
  294.      * @param currentIndex TODO
  295.      * @since VCafe 3.0
  296.      */
  297.  
  298.     protected int computeNextPanelIndex(int currentIndex)
  299.     {
  300.         int max;
  301.  
  302.         max = panels.size();
  303.  
  304.         if(max > currentIndex + 1)
  305.         {
  306.             return currentIndex + 1;
  307.         }
  308.  
  309.         return NULL_PANEL;
  310.     }
  311.  
  312.     /**
  313.      * @param currentIndex TODO
  314.      * @since VCafe 3.0
  315.      */
  316.  
  317.     protected int computePreviousPanelIndex(int currentIndex)
  318.     {
  319.         if (currentIndex > 0)
  320.         {
  321.             return currentIndex - 1;
  322.         }
  323.  
  324.         return NULL_PANEL;
  325.     }
  326.  
  327.     /**
  328.      * @param goHere TODO
  329.      * @exception java.lang.ArrayIndexOutOfBoundsException
  330.      * @since VCafe 3.0
  331.      */
  332.     protected int gotoPanel(JPanel goHere)
  333.         throws ArrayIndexOutOfBoundsException
  334.     {
  335.         int newIndex;
  336.  
  337.         newIndex = panels.indexOf(goHere);
  338.  
  339.         if((!(currentPanel instanceof WizardPage)) || (((WizardPage)currentPanel).exiting()))
  340.         {
  341.             currentPanelIndex = newIndex;
  342.             currentPanel = (JPanel)panels.elementAt(currentPanelIndex);
  343.  
  344.             if(currentPanel instanceof WizardPage)
  345.             {
  346.                 ((WizardPage)currentPanel).entering();
  347.             }
  348.  
  349.             if(wizard != null)
  350.             {
  351.                 wizard.showPanel(currentPanel);
  352.             }
  353.         }
  354.  
  355.         return newIndex;
  356.     }
  357.  
  358.     /**
  359.      * @since VCafe 3.0
  360.      */
  361.  
  362.     public void finish()
  363.     {
  364.         if((!(currentPanel instanceof WizardPage)) || ((WizardPage)currentPanel).exiting())
  365.         {
  366.             for(Enumeration e = panels.elements(); e.hasMoreElements();)
  367.             {
  368.                 JPanel panel;
  369.  
  370.                 panel = (JPanel)e.nextElement();
  371.  
  372.                 if(panel instanceof WizardPage)
  373.                 {
  374.                     ((WizardPage)panel).finish();
  375.                 }
  376.             }
  377.         }
  378.     }
  379.  
  380.     /**
  381.      * @since VCafe 3.0
  382.      */
  383.  
  384.     public void cancel()
  385.     {
  386.         if((!(currentPanel instanceof WizardPage)) || (((WizardPage)currentPanel).exiting()))
  387.         {
  388.             for(Enumeration e = panels.elements(); e.hasMoreElements();)
  389.             {
  390.                 JPanel panel;
  391.  
  392.                 panel = (JPanel)e.nextElement();
  393.  
  394.                 if(panel instanceof WizardPage)
  395.                 {
  396.                     ((WizardPage)panel).cancel();
  397.                 }
  398.             }
  399.         }
  400.     }
  401.  
  402.     /**
  403.      * @since VCafe 3.0
  404.      */
  405.  
  406.     public void cancelWizard()
  407.     {
  408.         if(wizard != null)
  409.         {
  410.             wizard.cancel();
  411.         }
  412.     }
  413.  
  414.     /**
  415.      * @since VCafe 3.0
  416.      */
  417.  
  418.     public void help()
  419.     {
  420.     }
  421.  
  422.     /**
  423.      * @param str TODO
  424.      * @since VCafe 3.0
  425.      */
  426.  
  427.     public void setWizardTitle(String str)
  428.     {
  429.         title = str;
  430.     }
  431.  
  432.     /**
  433.      * @since VCafe 3.0
  434.      */
  435.  
  436.     public String getWizardTitle()
  437.     {
  438.         return (title);
  439.     }
  440.  
  441.     /**
  442.      * @param dataName TODO
  443.      * @since VCafe 3.0
  444.      */
  445.  
  446.     public Object getData(String dataName)
  447.     {
  448.         for(Enumeration e = panels.elements(); e.hasMoreElements();)
  449.         {
  450.             JPanel panel;
  451.  
  452.             panel = (JPanel)e.nextElement();
  453.  
  454.             if(panel instanceof WizardPage)
  455.             {
  456.                 Object data;
  457.  
  458.                 data = ((WizardPage)panel).getData(dataName);
  459.  
  460.                 if(data != null)
  461.                 {
  462.                     return (data);
  463.                 }
  464.             }
  465.         }
  466.  
  467.         return (null);
  468.     }
  469.  
  470.     /**
  471.      * @since VCafe 3.0
  472.      */
  473.  
  474.     public Vector getSummary()
  475.     {
  476.         Vector summaries;
  477.  
  478.         summaries = new Vector();
  479.  
  480.         for(Enumeration e = panels.elements(); e.hasMoreElements();)
  481.         {
  482.             JPanel panel;
  483.  
  484.             panel = (JPanel)e.nextElement();
  485.  
  486.             if(panel instanceof WizardSummaryPage)
  487.             {
  488.                 Summary s;
  489.  
  490.                 s = ((WizardSummaryPage)panel).getSummary();
  491.  
  492.                 if(s != null)
  493.                 {
  494.                     summaries.addElement(s);
  495.                 }
  496.             }
  497.         }
  498.  
  499.         return (summaries);
  500.     }
  501.  
  502.     public Vector getWizardSummary()
  503.     {
  504.         Vector summaries;
  505.  
  506.         summaries = new Vector();
  507.  
  508.         for(Enumeration e = panels.elements(); e.hasMoreElements();)
  509.         {
  510.             JPanel panel;
  511.  
  512.             panel = (JPanel)e.nextElement();
  513.  
  514.             if(panel instanceof WizardSummaryPage)
  515.             {
  516.                 WizardSummary s;
  517.  
  518.                 s = ((WizardSummaryPage)panel).getWizardSummary();
  519.  
  520.                 if(s != null)
  521.                 {
  522.                     summaries.addElement(s);
  523.                 }
  524.             }
  525.         }
  526.  
  527.         return (summaries);
  528.     }
  529.  
  530.     /**
  531.      * @param f TODO
  532.      * @since VCafe 3.0
  533.      */
  534.  
  535.     public void setBackEnabled(boolean f)
  536.     {
  537.         if(currentPanelIndex > 0 && wizard != null)
  538.         {
  539.             wizard.setBackEnabled(f);
  540.         }
  541.     }
  542.  
  543.     /**
  544.      * @param f TODO
  545.      * @since VCafe 3.0
  546.      */
  547.  
  548.     public void setNextEnabled(boolean f)
  549.     {
  550.         if(currentPanelIndex < panels.size() && wizard != null)
  551.         {
  552.             wizard.setNextEnabled(f);
  553.         }
  554.     }
  555.  
  556.     /**
  557.      * @param f TODO
  558.      * @since VCafe 3.0
  559.      */
  560.  
  561.     public void setFinishEnabled(boolean f)
  562.     {
  563.         if(wizard != null)
  564.         {
  565.             wizard.setFinishEnabled(f);
  566.         }
  567.     }
  568.  
  569.     /**
  570.      * @param f TODO
  571.      * @since VCafe 3.0
  572.      */
  573.  
  574.     public void setCancelEnabled(boolean f)
  575.     {
  576.         if(wizard != null)
  577.         {
  578.             wizard.setCancelEnabled(f);
  579.         }
  580.     }
  581.  
  582.     /**
  583.      * @param f TODO
  584.      * @since VCafe 3.0
  585.      */
  586.  
  587.     public void setHelpEnabled(boolean f)
  588.     {
  589.         if(wizard != null)
  590.         {
  591.             wizard.setHelpEnabled(f);
  592.         }
  593.     }
  594.  
  595.     /**
  596.      * @since VCafe 3.0
  597.      */
  598.  
  599.     public boolean isBackEnabled()
  600.     {
  601.         if(wizard != null)
  602.         {
  603.             return (wizard.isBackEnabled());
  604.         }
  605.  
  606.         return (false);
  607.     }
  608.  
  609.     /**
  610.      * @since VCafe 3.0
  611.      */
  612.  
  613.     public boolean isNextEnabled()
  614.     {
  615.         if(wizard != null)
  616.         {
  617.             return (wizard.isNextEnabled());
  618.         }
  619.  
  620.         return (false);
  621.     }
  622.  
  623.     /**
  624.      * @since VCafe 3.0
  625.      */
  626.  
  627.     public boolean isFinishEnabled()
  628.     {
  629.         if(wizard != null)
  630.         {
  631.             return (wizard.isFinishEnabled());
  632.         }
  633.  
  634.         return (false);
  635.     }
  636.  
  637.     /**
  638.      * @since VCafe 3.0
  639.      */
  640.  
  641.     public boolean isCancelEnabled()
  642.     {
  643.         if(wizard != null)
  644.         {
  645.             return (wizard.isCancelEnabled());
  646.         }
  647.  
  648.         return (false);
  649.     }
  650.  
  651.     /**
  652.      * @since VCafe 3.0
  653.      */
  654.  
  655.     public boolean isHelpEnabled()
  656.     {
  657.         if(wizard != null)
  658.         {
  659.             return (wizard.isHelpEnabled());
  660.         }
  661.  
  662.         return (false);
  663.     }
  664.  
  665.     /**
  666.      * @since VCafe 3.0
  667.      */
  668.  
  669.     public void setBackFocus()
  670.     {
  671.         if(wizard != null)
  672.         {
  673.             wizard.setBackFocus();
  674.         }
  675.     }
  676.  
  677.     /**
  678.      * @since VCafe 3.0
  679.      */
  680.  
  681.     public void setNextFocus()
  682.     {
  683.         if(wizard != null)
  684.         {
  685.             wizard.setNextFocus();
  686.         }
  687.     }
  688.  
  689.     /**
  690.      * @since VCafe 3.0
  691.      */
  692.  
  693.     public void setFinishFocus()
  694.     {
  695.         if(wizard != null)
  696.         {
  697.             wizard.setFinishFocus();
  698.         }
  699.     }
  700.  
  701.     /**
  702.      * @since VCafe 3.0
  703.      */
  704.  
  705.     public void setCancelFocus()
  706.     {
  707.         if(wizard != null)
  708.         {
  709.             wizard.setCancelFocus();
  710.         }
  711.     }
  712.  
  713.     /**
  714.      * @since VCafe 3.0
  715.      */
  716.  
  717.     public void setHelpFocus()
  718.     {
  719.         if(wizard != null)
  720.         {
  721.             wizard.setHelpFocus();
  722.         }
  723.     }
  724.     
  725.     public void setImage(Image image)
  726.     {
  727.         if(wizard != null) 
  728.         {
  729.             wizard.setImage(image);
  730.         }
  731.     }
  732.     
  733.     public void setImage(URL url)
  734.     {
  735.         if(wizard != null) 
  736.         {
  737.             wizard.setImage(url);
  738.         }
  739.     }
  740.     
  741.     public void setImage(InputStream stream)
  742.         throws IOException
  743.     {
  744.         if(wizard != null) 
  745.         {
  746.             wizard.setImage(stream);
  747.         }
  748.     }
  749.     
  750.     public void setImage(ImageIcon icon)
  751.     {
  752.         if(wizard != null)
  753.         {
  754.             wizard.setImage(icon);
  755.         }
  756.     }
  757. }